package com.hkbigdata.flink.utils;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author JinChen Zhu
 * @Date 2024/4/23 9:58
 */
public class PropertiesUtils {
    private static Properties props = new Properties();
    private static Map<String, String> PROPER_MAP = new ConcurrentHashMap();

    static {
        InputStream is = null;
        try {
            // 获取classpath下的application.properties文件
            is = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties");
            // 加载Properties文件
            if (is != null) {
                props.load(new InputStreamReader(is, "GBK"));
            }
            // 将Properties对象中的属性值存储到Map中
            Iterator<Object> iterator = props.keySet().iterator();

            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                PROPER_MAP.put(key, props.getProperty(key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getProperty(String key) {
        return PROPER_MAP.get(key);
    }

    public static void main(String[] args) {
        // 测试获取属性值
        String value = getProperty("mysql.database");
        System.out.println(value);
    }


}
